﻿using LaoLuo;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LaoLuo.Common.Test
{
    
    
    /// <summary>
    ///这是 StringExtensionsTest 的测试类，旨在
    ///包含所有 StringExtensionsTest 单元测试
    ///</summary>
    [TestClass()]
    public class StringExtensionsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///Count 的测试
        ///</summary>
        [TestMethod()]
        public void CountTest()
        {
            Assert.AreEqual(3, StringExtensions.Count("1212121212121", "121"));
            Assert.AreEqual(5, StringExtensions.Count("11111", "1"));
        }

        [TestMethod()]
        public void GetNumericPartTest()
        {
            Assert.AreEqual(-123, "sdfsd-123fdsf".GetNumericPart<int>());
            Assert.AreEqual(-1239999999999, "sdfsd-1239999999999fdsf".GetNumericPart<long>());
            Assert.AreEqual(1.11231321321321321, "sdfsd1.11231321321321321fdsf".GetNumericPart<double>());
            Assert.AreEqual(-1.1m, "sdfsd-1.1fdsf".GetNumericPart<decimal>());
        }

        [TestMethod()]
        public void GetNumericPartsTest()
        {
            AssertHelper.AreEqual("sdfsd-123.2f2.3423d-2sf".GetNumericParts<double>(), -123.2, 2.3423, -2);
            AssertHelper.AreEqual("sdfsd-123f23423d2sf".GetNumericParts<int>(), -123, 23423, 2);
            AssertHelper.AreEqual("sdfsd-123.2f2.3423d-2sf".GetNumericParts<decimal>(), -123.2m, 2.3423m, -2m);
        }

        

        /// <summary>
        ///RegRemoveWhiteSpace 的测试
        ///</summary>
        [TestMethod()]
        public void RegRemoveWhiteSpaceTest()
        {
            Assert.AreEqual("0123", "0 1  2   3".RegRemoveWhiteSpace());
            Assert.AreEqual("0123", "   0 1  2   3".RegRemoveWhiteSpace());
            Assert.AreEqual("0123", " 0 1  2   3   ".RegRemoveWhiteSpace());
            Assert.AreEqual("0123", "0 1  2   3   ".RegRemoveWhiteSpace());
        }

        /// <summary>
        ///Truncate 的测试
        ///</summary>
        [TestMethod]
        public void TruncateTest()
        {
            Assert.AreEqual("abc", "abc".Truncate(1000));
            Assert.AreEqual("a", "abcd".Truncate(1));
            Assert.AreEqual("", "abc".Truncate(0));
        }

        /// <summary>
        ///TruncateUtf8 的测试
        ///</summary>
        [TestMethod]
        public void TruncateUtf8Test()
        {
            Assert.AreEqual("abc", "abc".TruncateUtf8(1000));
            Assert.AreEqual("a", "abcd".TruncateUtf8(1));
            Assert.AreEqual("", "abc".TruncateUtf8(0));

            Assert.AreEqual("", "的的的".TruncateUtf8(1));
            Assert.AreEqual("的", "的的的".TruncateUtf8(2));
            Assert.AreEqual("的的", "的的的".TruncateUtf8(4));
            Assert.AreEqual("的的", "的的的".TruncateUtf8(5));
            Assert.AreEqual("的的,", "的的,的".TruncateUtf8(5));
            Assert.AreEqual("的的的", "的的的".TruncateUtf8(10000));


            Assert.AreEqual("的的...", "的的的".TruncateUtf8(4, "..."));
        }
    }
}
